package com.liwalo.dao;

// Generated 24-ago-2013 14:46:30 by Hibernate Tools 3.6.0

import static org.hibernate.criterion.Example.create;

import java.util.List;

import javax.naming.InitialContext;
import org.hibernate.Transaction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.liwalo.entities.Atributo;
import com.liwalo.entities.ImgProducto;
import com.liwalo.entities.MediosContacto;
import com.liwalo.entities.Usuario;
import com.liwalo.utils.HibernateUtil;

/**
 * Home object for domain model class MediosContacto.
 * @see com.liwalo.dao.MediosContacto
 * @author Hibernate Tools
 */
public class MediosContactoDAO {

	private static final Log log = LogFactory.getLog(MediosContactoDAO.class);

	private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			return (SessionFactory) new InitialContext()
					.lookup("SessionFactory");
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	public void persist(MediosContacto transientInstance) {
		log.debug("persisting MediosContacto instance");
		  Session session = sessionFactory.getCurrentSession();
	        Transaction tx = session.beginTransaction();
		try {
			 session.save(transientInstance);
	         tx.commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(MediosContacto instance) {
		log.debug("attaching dirty MediosContacto instance");
		 Session session = sessionFactory.getCurrentSession();
	     Transaction tx = session.beginTransaction();
		try {
			
			session.saveOrUpdate(instance);
	         tx.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(MediosContacto instance) {
		log.debug("attaching clean MediosContacto instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(MediosContacto persistentInstance) {
		log.debug("deleting MediosContacto instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public MediosContacto merge(MediosContacto detachedInstance) {
		log.debug("merging MediosContacto instance");
		try {
			MediosContacto result = (MediosContacto) sessionFactory
					.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public MediosContacto findById(java.lang.Integer id) {
		log.debug("getting MediosContacto instance with id: " + id);
		 Session session = sessionFactory.getCurrentSession();
	     Transaction tx = session.beginTransaction();
		try {
			MediosContacto instance = (MediosContacto) session.get(MediosContacto.class,
							id);
			 tx.commit();
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<MediosContacto> findByExample(MediosContacto instance) {
		log.debug("finding MediosContacto instance by example");
		try {
			List<MediosContacto> results = (List<MediosContacto>) sessionFactory
					.getCurrentSession()
					.createCriteria("com.liwalo.dao.MediosContacto")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public MediosContacto findById(Usuario usuariovo) {
		log.debug("getting Usuario instance with id: " + usuariovo);
		List<MediosContacto> result = null;
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		try{
            
           

            Criteria criteria = session.createCriteria(MediosContacto.class , "medio")
					.add(Restrictions.eq("medio.usuarioId", usuariovo))
					.add(Restrictions.eq("medio.estado", 1));
            result = criteria.list();
            


            if(result != null && result.size()>0){
            	MediosContacto medios=(MediosContacto)result.get(0);
            	initLazy(medios);
                return medios;
            }



		} catch (RuntimeException re) {
			throw re;
		}


            return null;
	}
	
	public List<MediosContacto> ListfindById(Usuario usuariovo) {
		log.debug("getting Usuario instance with id: " + usuariovo);
		Session session = sessionFactory.getCurrentSession();
		try {
			session.beginTransaction();
			List<MediosContacto> instance = (List<MediosContacto>)session.
					createCriteria(MediosContacto.class , "medio")
					.add(Restrictions.eq("medio.usuarioId", usuariovo))
					.add(Restrictions.eq("medio.estado", 1)).list()
					
					;
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	
	 private void initLazy(MediosContacto medios){
		//Inicializo lazy
	        if(medios!=null){
	        	if(!Hibernate.isInitialized(medios.getListadoCiudadId())){
	        		Hibernate.initialize(medios.getListadoCiudadId());
	        	}
	        	if(!Hibernate.isInitialized(medios.getListadoPaisId())){
	        		Hibernate.initialize(medios.getListadoPaisId());
	        	}
	        	if(!Hibernate.isInitialized(medios.getListadoProvId())){
	        		Hibernate.initialize(medios.getListadoProvId());
	        	}
	        	if(!Hibernate.isInitialized(medios.getTipoViaId())){
	        		Hibernate.initialize(medios.getTipoViaId());
	        	}
	        	
	        }
		 
	 }
	
	
}
